package com.cn.lycss.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.lycss.model.ResultModel;
import com.cn.lycss.model.UserModel;
import com.cn.lycss.service.UserService;
import com.cn.lycss.service.impl.UserServiceImpl;
import com.cn.lycss.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/user/add")
public class UserAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //        1.得到前端传递过来的数据
        String username = req.getParameter("username");
        String password = req.getParameter("pass");
        String email = req.getParameter("email");
        String birthday = req.getParameter("birthday");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String hobby = req.getParameter("hobby");
        Integer enable = Integer.valueOf(req.getParameter("enable"));


        //        2.将数据封装到model
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPassword(Tool.getMD5String(password));
        userModel.setSex(sex);
        userModel.setBirthday(birthday);
        userModel.setEnable(enable);
        userModel.setEmail(email);
        userModel.setHobby(hobby);
        userModel.setPhone(phone);
        //        3.创建服务逻辑层
        UserService userService = new UserServiceImpl();
        //        4.接收服务逻辑层返回信息

        ResultModel resultModel = userService.add(userModel);
        //        5.将信息返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultModel));
    }
}
